package com.threeonemeeting.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.tab.BaseTabActivity;

public class MeetingWebViewActivity extends BaseTabActivity {
	private WebView mWebView = null;

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_oauth_webview);
		init("31会议");

		showLoadingLayout("正在打开网页");

		String strUrl = getIntent().getExtras().getString("url");
		if (TextUtils.isEmpty(strUrl) == true) {
			finish();
			return;
		}

		mWebView = (WebView) findViewById(R.id.web);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setPluginsEnabled(true);
		mWebView.setVerticalScrollBarEnabled(true);
		mWebView.setHorizontalScrollBarEnabled(true);
		mWebView.getSettings().setDefaultFontSize(16);
		mWebView.getSettings().setBuiltInZoomControls(true); // 显示放大缩小 controler
		mWebView.getSettings().setSupportZoom(true); // 可以缩放
		mWebView.getSettings().setLoadWithOverviewMode(true);
		mWebView.setInitialScale(100);
		mWebView.loadUrl(strUrl);
		mWebView.setWebViewClient(new WebViewClient() {

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String strUrl) {
				view.loadUrl(strUrl);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);

				showWebView();
			}
		});
	}

	private void showLoadingLayout(String strHint) {

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView webViewLayout = (ScrollView) findViewById(R.id.layout_webview);

		if (loadingLayout != null) {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		if (webViewLayout != null) {
			webViewLayout.setVisibility(View.GONE);
		}

		TextView loadingText = (TextView) findViewById(R.id.loading_text);
		if (loadingText != null) {
			loadingText.setText(strHint);
		}
	}

	private void showWebView() {
		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		ScrollView webViewLayout = (ScrollView) findViewById(R.id.layout_webview);

		if (loadingLayout != null) {
			loadingLayout.setVisibility(View.GONE);
		}

		if (webViewLayout != null) {
			webViewLayout.setVisibility(View.VISIBLE);
		}
	}
}
